package think.thread.sychron.c01.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CarLock {

	Lock lock = new ReentrantLock();
	Condition condition = lock.newCondition();

	private boolean waxOn = false;

	// 打蜡
	public void waxed() {
		try {
			lock.lock();
			waxOn = true;
			condition.signalAll();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}

	}

	// 抛光
	public void buffed() {
		try {
			lock.lock();
			waxOn = false;
			condition.signalAll();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}

	}

	public void waitForWaxing() throws Exception {

		try {
			lock.lock();
			while (!waxOn)
				condition.await();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}

	}

	public void waitForBuff() throws Exception {
		try {
			lock.lock();
			while (waxOn)
				condition.await();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}

	}

}
